<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var largestMerge = function (word1, word2) {
      let merge = '';
      let i = 0, j = 0;
      while (i < word1.length || j < word2.length) {
        if (i < word1.length && word1.slice(i) > word2.slice(j)) {
          merge += word1[i];
          i++;
        } else {
          merge += word2[j];
          j++;
        }
      }
      return merge;
    };
    console.log(largestMerge("cabaa", "bcaaa"));
  </script>
</body>

</html>